package com.lanou3g.code0512.request;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

/**
 * If there are no bugs, it was created by Chen FengYao on 2018/5/12;
 * Otherwise, I don't know who created it either
 */

@WebServlet(name = "DServlet",urlPatterns = "/d")
public class DServlet extends HttpServlet {



    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setContentType("text/html;charset=utf-8");
        /*
        request 封装了http请求的所有数据
        请求行
        请求头
        空行
        请求体(get请求没有)
         */

        // 1. 获取客户端ip
        // 用来 实现 封ip的功能
        String remoteAddr = request.getRemoteAddr();
//
//        if ("172.16.16.114".equals(remoteAddr)){
//            response.getWriter().println("正常提供服务");
//        }else {
//            response.getWriter().println("你已经被封ip了");
//        }
//
//        if (true) return;

        // 2. 获取请求方式
        String method = request.getMethod();

        PrintWriter writer = response.getWriter();

        //请求头
        // 我们只会 拿浏览器自动添加的头,自己添加的头
        // 一会用postman测试

        // 判断用户的 浏览器/操作系统
        String userAgent = request.getHeader("user-agent");
        // 当页面发起请求时,会自动添加从哪个域名发起的请求
        // 所以 可以用来做 防盗链
        // 但是 现在基本上 都用 cookie/session 配合referer来做
        String referer = request.getHeader("referer");

        // 自定义头
        String myHead = request.getHeader("myHead");

        writer.println("myHead:" + myHead);

        writer.println(remoteAddr);
        writer.println(method);

        writer.println(userAgent);
        writer.println(referer);

    }
}
